package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.CommunicateLogEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CustomerAddCommActivity extends BaseActivity{
    private Spinner spinner_name;
    private EditText edit_content;
    private Button btn_show;
    private TextView txt_next_time;
    private ImageView img_back,img_commit;
    private TimePickerView mpv;
    String next_time;
    List<CommunicateLogEntity>list;
    private SharedPreferences sp;
    private List<String> name_list=new ArrayList<>();
    private ArrayAdapter adapter1;
    private String manager_name;
    private int manager_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coording_add_communication);
        img_back= (ImageView) findViewById(R.id.coording_communication_add_img_back);
        img_commit= (ImageView) findViewById(R.id.coording_communication_add_img_commit);
        spinner_name= (Spinner) findViewById(R.id.coording_communication_add_spinner_name);
        txt_next_time= (TextView) findViewById(R.id.coording_communication_add_next_time);
        edit_content= (EditText) findViewById(R.id.coording_communication_add_edit_content);
        btn_show= (Button) findViewById(R.id.coording_communication_add_btn_show);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                next_time=getTime(date);
                txt_next_time.setText(next_time);
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                list=new ArrayList<CommunicateLogEntity>();
                dockDao dockDao=new dockDaoImpl();
                SC sc=new SC();
                JSONObject object=sc.storage(sp);
                String json=object.toString();
                list=dockDao.queryCoordingCommunicationMnager(baseClass.coording_communication_spinner,json);
                for (int i2=0;i2<list.size();i2++){
                    name_list.add( list.get(i2).getCommunication_manager_name());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter1 = new ArrayAdapter(CustomerAddCommActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,name_list);
                        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_name.setAdapter(adapter1);
                        spinner_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                manager_name=spinner_name.getSelectedItem().toString();
                                for (int i1 = 0;i1<list.size();i1++){
                                    if (manager_name.equals(list.get(i1).getCommunication_manager_name())){
                                        manager_id = Integer.valueOf(list.get(i1).getCommunication_manager_id());
                                    }
                                    Log.d("kii",manager_id+"");
                                    Log.d("fff",manager_name);
                                }

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                    }
                });

            }
        }).start();
    }
    public void add(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc=new SC();
                CustomerMessageEntity customer = GetCustumer.customers;

                JSONObject object=sc.storage(sp);
                try {
                    object.put("KAA01",customer.getClient_id());
                    Log.d("ccid",customer.getClient_id()+"");
                    object.put("KGA03",edit_content.getText().toString());
                    Log.d("oopp",txt_next_time.getText().toString());
                    object.put("KGA04",txt_next_time.getText().toString());
                    object.put("KGA07",manager_id);
                    object.put("KGA08",manager_name);
                    object.put("addoredit","add");
                    object.put("CID03",sp.getString("CID03",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String JSON=object.toString();
                String result= NetWorkUtils.getCount(baseClass.add_customer_comm,JSON);
                try {
                    JSONObject json=new JSONObject(result);
                    String boolen=json.getString("ifsuccess");
                    if ("true".equals(boolen)){
                        if ("detil".equals(getIntent().getStringExtra("TAG"))){
                            Intent intent = new Intent(CustomerAddCommActivity.this, CustomerSimpleCommunicateLogActivity.class);
                            intent.putExtra("customerid",customer.getClient_id());
                            startActivity(intent);
                        }else {

                        }
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
